package com.gsx.Controller;

import com.github.pagehelper.PageInfo;
import com.gsx.entity.Blog;
import com.gsx.entity.Blog_Type;
import com.gsx.entity.UserInfo;
import com.gsx.service.BlogService;
import com.gsx.service.TagService;
import com.gsx.service.TypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/admain")
public class BlogController {
    @Autowired
    private BlogService blogService;

    @Autowired
    private TypeService typeService;

    @Autowired
    private TagService tagService;
    /*查看表*/
    @GetMapping("/blogs")
    public String blogs(@RequestParam(name="page",required = true,defaultValue = "0")int page,
                        @RequestParam(name="size",required = true,defaultValue = "10")int size, Model model,
                        Blog_Type blog_type){
        List<Blog_Type> list;
        list= blogService.getListBlog(page,size,blog_type);
        PageInfo pageInfo =new PageInfo(list);
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("types",typeService.getAll());
        System.out.println("-----------------------------------");
        return "admain/blogs";
    }

    @PostMapping("/blogs/search")
    public String search(@RequestParam(name="page",required = true,defaultValue = "0")int page,
                         @RequestParam(name="size",required = true,defaultValue = "10")int size, Model model,
                         Blog_Type blog_type){
        List<Blog_Type> list = null;
        if(blog_type.getTitle().equals("")){
            blog_type.setTitle(null);
            list=blogService.getListBlog(page, size, blog_type);
        }else {
            list= blogService.getListBlog(page,size,blog_type);
        }
        model.addAttribute("pageInfo",new PageInfo(list));
        return "admain/blogs :: blogList";
    }


    /*发布博客*/
    @GetMapping("/blogs/input")
    public String input(Model model){
        model.addAttribute("types",typeService.getAll());
        model.addAttribute("tags",tagService.getAll());
        model.addAttribute("blog",new Blog());
        return "admain/blogs-input";
    }

    @PostMapping("/blogs")
    public String post(Blog blog,String tag_id ,RedirectAttributes attributes, HttpSession session){
        UserInfo userInfo =(UserInfo)session.getAttribute("user");
        blog.setUser_id(userInfo.getUser_id());
        int result=blogService.saveBlog(blog);
        /*获取所有id号*/
        Long[] blog_ids=blogService.getId();
        System.out.println(blog_ids[blog_ids.length-1]);
        tagService.bt_idInsert(blog_ids[blog_ids.length-1], tag_id);
        if(result==0){
            attributes.addFlashAttribute("message","操作失败");
        }else {
            attributes.addFlashAttribute("message","操作成功");
        }
        return "redirect:/admain/blogs";
    }


    /*修改*/
    @GetMapping("/blogs/{id}/input")
    public String edit(Model model,@PathVariable Long id){
        model.addAttribute("types",typeService.getAll());
        model.addAttribute("tags",tagService.getAll());
        model.addAttribute("blog",blogService.getBlog(id));
        model.addAttribute("blog_id",id);
        return "admain/blogs-input";
    }
    //title,content,type_id,tag_id,firstPicture,recommend,shareStatement,appreciation,commentable
    @PostMapping("/blogs/{id}")
    public String editInput(RedirectAttributes attributes,@PathVariable Long id,Blog newBlog,String tag_id){
        Blog blog=blogService.getBlog(id);
        newBlog.setBlog_id(id);
        newBlog.setCreateTime(blog.getCreateTime());
        newBlog.setViews(blog.getViews());
        newBlog.setUpdateTime(new Date());
        newBlog.setUser_id(blog.getUser_id());
        int result=blogService.updateBlog(newBlog);
        //删除再插入tag
        System.out.println(tag_id);
        tagService.delBlog_tag(id);
        tagService.bt_idInsert(id,tag_id);

        if(result==0){
            attributes.addFlashAttribute("message","操作失败");
        }else {
            attributes.addFlashAttribute("message","操作成功");
        }
        return "redirect:/admain/blogs";
    }


    /*删除指定的博客*/
    @GetMapping("blogs/{id}/delete")
    public String delBlog(RedirectAttributes attributes,@PathVariable Long id){
        tagService.delBlog_tag(id);
        int result=blogService.deleteBlog(id);
        if(result==0){
            attributes.addFlashAttribute("message","操作失败");
        }else {
            attributes.addFlashAttribute("message","操作成功");
        }
        return "redirect:/admain/blogs";
    }
}
